package com.example.teamtribe.main.club.clubintroduce.update;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.common.model.TeamModel;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.event.Refresh;
import com.example.teamtribe.main.club.clubintroduce.media.MediaModel;
import com.example.teamtribe.utils.Utility;

import org.greenrobot.eventbus.EventBus;
import org.litepal.crud.DataSupport;

import java.io.File;
import java.util.List;

public class UpdatePresenter implements UpdateContract.Presenter {

    private UpdateContract.View view;
    private UpdateModel model;

    public UpdatePresenter(UpdateContract.View view) {
        this.view = view;
        model = new UpdateModel();
    }

    @Override
    public void update(File head, String clubname,String comment, String portrait, int id,String wallpaper) {
        if (TextUtils.isEmpty(clubname)) {
            view.showToast("社团名称不能为空");
            return;
        }
        view.showProgress();
        if (head == null) {
            updateInfo(clubname, comment,portrait, id,wallpaper);
        } else {
            uploadHead(head, clubname, comment, id,wallpaper);
        }
    }

    @Override
    public void updateMedia(final String name, final int teamId, final File cover, final String description, final int type) {
        view.showProgress();
        model.uploadHead(Utility.getToken(),cover,new Back(){
            @Override
            public void success(@Nullable Bundle bundle) {
                MediaModel.getInstance().addMedia(name, teamId, bundle.getString("fileName"), description, type, new Back() {
                    @Override
                    public void success(@Nullable Bundle bundle) {
//                        int
//                        MediaModel.getInstance().getMedia(mediaId, new FileModel.FileBack() {
//                            @Override
//                            public void loadSuccess(String fileName) {
//                                view.next();
//                            }
//
//                            @Override
//                            public void success(@Nullable Bundle bundle) {
//                                Team team = (Team) bundle.getParcelable("team");
//                                team.saveOrUpdate();
//                                List<Team> teams = DataSupport.where("realId = ?", team.getRealId() + "").find(Team.class);
//                                Team a = teams.get(0);
//                            }
//
//                            @Override
//                            public void failure(String meg) {
//
//                            }
//                        });
                    }

                    @Override
                    public void failure(String meg) {

                    }
                });
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }

        });
    }

    private void uploadHead(File head, final String clubname, final String comment,final int id,final String wallpaper) {
        model.uploadHead(Utility.getToken(), head, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                updateInfo(clubname, comment, bundle.getString("fileName"), id,wallpaper);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void updateInfo(String clubname, String comment,String portrait, final int id,String wallpaper) {
        model.updateInfo(Utility.getToken(), clubname, comment, portrait, id, wallpaper,new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                TeamModel.getInstance().getTeam(id, new FileModel.FileBack() {
                    @Override
                    public void loadSuccess(String fileName) {
                        EventBus.getDefault().post(new Refresh(Refresh.CLUBFRAGMENT));
                        view.next();
                    }

                    @Override
                    public void success(@Nullable Bundle bundle) {
                        Team team=(Team) bundle.getParcelable("team");
                        team.saveOrUpdate();
                        List<Team> teams = DataSupport.where("realId = ?", team.getRealId() + "").find(Team.class);
                        Team a=teams.get(0);
                    }

                    @Override
                    public void failure(String meg) {
                        view.hideProgress();
                        view.showToast(meg);
                    }
                });
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }
}